package com.yogurt.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yogurt.edu.entity.Course;
import com.yogurt.edu.entity.CourseDescription;
import com.yogurt.edu.entity.vo.CourseInfoVo;
import com.yogurt.edu.entity.vo.CoursePublishVo;
import com.yogurt.edu.entity.vo.CourseQuery;
import com.yogurt.edu.mapper.CourseMapper;
import com.yogurt.edu.service.ChapterService;
import com.yogurt.edu.service.CourseDescriptionService;
import com.yogurt.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yogurt.edu.service.VideoService;
import com.yogurt.servicebase.handler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yogurt
 * @since 2020-07-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    //注入操作课程描述的service
    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ChapterService chapterService;

    //添加课程信息
    @Override
    public String addCourse(CourseInfoVo courseInfoVo) {
        //创建课程对象
        Course course = new Course();
        /*传过来的json数据封装在courseInfoVo对象中
          需要将courseInfoVo对象中的数据取出并设置到Course对象中
          所以使用BeanUtils
         */
        BeanUtils.copyProperties(courseInfoVo,course);

        int i = baseMapper.insert(course); //添加数据后会自动生成id
        if (i == 0){
            throw new MyException(20001,"添加课程信息失败");
        }
        /*课程(Course)和课程描述(CourseDescription)之间是一对一关系,一门课程对应一个课程描述
          为了保持是对应的一致性，需要课程的id和课程描述的id一致
        */

        //得到新插入数据(课程)的id
        String courseId = course.getId();

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.save(courseDescription);
        return courseId;
    }

    //根据课程id查询课程信息和描述信息
    @Override
    public CourseInfoVo findCourseById(String courseId) {
        //根据课程id查询课程信息
        Course course = baseMapper.selectById(courseId);
        //根据课程id查询课程描述信息
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        //将课程信息封装到CourseInfoVo
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);
        //将课程描述信息封装到CourseInfoVo
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    //修改课程信息和课程描述信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        //将courseInfoVo对象中的信息封装到course对象中
        BeanUtils.copyProperties(courseInfoVo,course);
        //修改课程表
        int update = baseMapper.updateById(course);
        if (update == 0){
            throw new MyException(20001,"课程信息修改失败");
        }
        //修改描述表
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    //根据课程id查询课程的基本信息(多表查询)
    @Override
    public CoursePublishVo getCoursePublishInfo(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublish(courseId);
        return coursePublishVo;
    }

    //根据课程id删除课程
    @Override
    public void deleteById(String courseId) {

        // 1.根据课程id删除小节
        videoService.deleteVideoByCourseId(courseId);
        // 2.根据课程id删除章节
        chapterService.deleteChapterByCourseId(courseId);
        // 3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        // 4.根据课程id删除课程本身
        int i = baseMapper.deleteById(courseId);
        if (i == 0){
            throw new MyException(20001,"删除课程失败");
        }
    }
}
